package com.zeedev.flyingsaucer.data.wrappers;

import android.content.SharedPreferences;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import mobi.zeedev.flyingsaucer.common.CredentialsModule.impl.Credentials;
import mobi.zeedev.flyingsaucer.common.CredentialsModule.interfaces.ICredentials;
import mobi.zeedev.flyingsaucer.common.CredentialsModule.interfaces.IPreferenceCredentials;

import java.util.Date;

/**
 * User: James
 * Date: 1/29/11
 * Time: 10:31 AM
 */
public class PreferenceCredentials implements IPreferenceCredentials {

    private final ICredentials credentials;

    private final SharedPreferences preferences;

    @Inject
    public PreferenceCredentials(@Named("autoPreferenceCredentials") ICredentials credentials, SharedPreferences preferences) {
        this.credentials = credentials;
        this.preferences = preferences;
    }

    @Override
    public synchronized ICredentials getCredentials() {
        return credentials;
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Bindings
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Inject
    public synchronized void bindFromPreferences() {              //@Named("lastMemberId") Integer memeberId
//		if (memeberId == null || memeberId == 0) {
//			// check if exists
//			// do not bind and leave defaults alone
//			return;
//		}


//        SharedPreferences preferences = mContext.getSharedPreferences(generatePreferencesName(memeberId), Context.MODE_PRIVATE);


        // String storeName = preferences.getString("login_Store", "");

        // preferences.getString("login_F", "f");

        credentials.setStoreNumber(preferences.getString(Credentials.STORE_NUMBER, ""));
        credentials.setCardNumber(preferences.getString(Credentials.CARD_NUMBER, ""));
        credentials.setPassword(preferences.getString(Credentials.PASSWORD, ""));

        credentials.setUfoAutoLogin(preferences.getBoolean(Credentials.UFO_AUTO_LOGIN, false));
        credentials.setUfoRemember(preferences.getBoolean(Credentials.UFO_REMEMBER, false));
        credentials.setUfoMouMember(preferences.getBoolean(Credentials.UFO_MOU_MEMBER, false));
        Long time = preferences.getLong(Credentials.UFO_ORIGIN_DATE, 0l);
        if (time == 0) {
            credentials.setOriginDate(null);
        } else {
            Date d = new Date();
            d.setTime(time);
            credentials.setOriginDate(d);
        }

        credentials.setUserName(preferences.getString(Credentials.USER_NAME, ""));
        Integer testInt = preferences.getInt(Credentials.MEMBER_ID, 0);
        credentials.setMemberId(testInt);

    }

    @Override             //Integer memberIdIn
    public synchronized void bindToPreferences() {
//        String USER_PREFS_NAME = generatePreferencesName(memberIdIn);

//        SharedPreferences preferences = mContext.getSharedPreferences(USER_PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        editor.putString(Credentials.STORE_NUMBER, credentials.getStoreNumber());
        editor.putString(Credentials.CARD_NUMBER, credentials.getCardNumber());
        editor.putString(Credentials.PASSWORD, credentials.getPassword());
        editor.putString(Credentials.USER_NAME, credentials.getUserName());
        editor.putString(Credentials.PASSWORD_HASH, credentials.getPasswordHash());
        editor.putBoolean(Credentials.UFO_AUTO_LOGIN, credentials.getUfoAutoLogin());
        editor.putBoolean(Credentials.UFO_REMEMBER, credentials.getUfoRemember());
        editor.putInt(Credentials.MEMBER_ID, credentials.getMemberId());
        editor.putLong(Credentials.UFO_ORIGIN_DATE, credentials.getOriginDate() != null ? credentials.getOriginDate().getTime() : 0l);
        editor.putBoolean(Credentials.UFO_MOU_MEMBER, credentials.getUfoMouMember());

        editor.commit();

    }


/*    public synchronized void bindToPreferencesWithDefaultMemberId() {
        bindToPreferences(credentials.getMemberId());
    }*/


/*    public synchronized void bindConditionallyToPreferences() {
        if (credentials.getUfoRemember()) {
            bindToPreferences();//credentials.getMemberId
        }
    }*/

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Generators
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
/*
    private static String generatePreferencesName(Integer memberId) {
        return memberId + ".preferences";
    }*/

}
